package com.stupidzhe.vbreview.controller.adm;

import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.domain.Student;
import com.stupidzhe.vbreview.domain.Teacher;
import com.stupidzhe.vbreview.service.StudentService;
import com.stupidzhe.vbreview.service.UploadProjectService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/adm/uploadProject")
public class AdmUploadProjectController {

    private final UploadProjectService uploadProjectService;

    private final StudentService studentService;

    public AdmUploadProjectController(UploadProjectService uploadProjectService, StudentService studentService) {
        this.uploadProjectService = uploadProjectService;
        this.studentService = studentService;
    }

    @RequestMapping({"", "/"})
    public String mainView(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                           @RequestParam(required = false) String keyword, @RequestParam(required = false) Integer studentId, Model model, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getAttribute("teacher");
        model.addAttribute("pageBean", uploadProjectService.getUploadProjectByPage(pageNum, teacher.getUserId(), keyword, studentId));
        if (studentId != null) {
            Student student = studentService.selectById(studentId);
            model.addAttribute("studentId", studentId);
            model.addAttribute("studentNum", student.getStudentNum());
        }
        model.addAttribute("keyword", keyword);
        return "adm/adm.upload";
    }

    /**
     * 手动通过
     */
    @ResponseBody
    @RequestMapping("/{id}")
    public ResultBean edit(@RequestParam("result") String result, @PathVariable("id") Integer uploadId, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getAttribute("teacher");
        return uploadProjectService.changeResult(teacher.getUserId(), uploadId, result);
    }
}
